Código fuente de 'Muestra fragmentos de texto.asp'

<html>

<head>
<title>Muestra fragmentos de texto - Códigos asp, programacion asp, descargas asp, rutinas asp</title>
</head>

<body style="font-family: Arial; font-size: 11pt">
<center><b><font face="Arial" size="3">Muestra fragmentos de texto</font></b></center><br><br>

Esta función permite, dada una cadena y una longitud "n" mostrar fragmentos de ella 
(sin cortar palabras) siempre que la longitud no exceda de "n".
La función permite además añadir caracteres u otra cadena al final de la cadena original. 

<%
Function CropSentence(cadena, Longitud, cadFinal)
	Dim Posicion 'As Integer
	Dim MaxLong 'As Integer
	Dim cadSalida 'As String

	'Set the max length to be the desired length minus 
	'the length of the trailing characters.
	MaxLong = Longitud - Len(cadFinal)

	'Check if the max length is less than or equal to zero.
	If MaxLong <= 0 Then
		'Return an empty string.
		CropSentence = ""

	'Check if the inputted text it is less that the desired length.
	ElseIf Len(cadena) <= Longitud Then
		'Return the inputted text
		CropSentence = cadena

	'Crop the sentence
	Else
		'Remove the end of the text.
		cadSalida = Left(cadena, Longitud)

		'Get the position of the last space in the text.
		Posicion = InStrRev(cadSalida, " ")

		'Return the cropped sentance
		CropSentence = Left(cadSalida, Posicion - 1) & cadFinal
	End If
End Function

response.write "<br><br><b>Ejemplo: </b> CropSentence(""Astalaweb.com - Rutinas para programadores"",14,""-cadena final"")"
response.write "<br><b>Resultado: </b> " & CropSentence("Astalaweb.com - Rutinas para programadores",14,"-cadena final")
%>

</body></html>